package com.xiyuan.service.impl;


import com.xiyuan.constant.MessageConstant;
import com.xiyuan.mapper.MemoCategoryMapper;
import com.xiyuan.pojo.entity.MemoCategory;
import com.xiyuan.service.MemoCategoryService;
import com.xiyuan.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class MemoCategoryServiceImpl implements MemoCategoryService {
    @Autowired
    private MemoCategoryMapper memoCategoryMapper;

    @Override
    public int insert(MemoCategory memoCategory) {
        //获取当前登录用户
        Map map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        memoCategory.setUserId(userId);
        return memoCategoryMapper.insert(memoCategory);
    }

    @Override
    public List<MemoCategory> getList() {
        //获取当前登录用户
        Map map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        return memoCategoryMapper.getList(userId);
    }

    @Override
    public void update(MemoCategory memoCategory) {
        //判断更新分类的用户是否与当前登录用户一致
        Map map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        if (memoCategory.getUserId() != userId){
            throw new RuntimeException(MessageConstant.UPDATE_FAIL);
        }
        memoCategoryMapper.update(memoCategory);
    }

    /**
     * 根据id查询分类
     * @param id
     * @return
     */
    @Override
    public MemoCategory findById(Integer id) {
        return memoCategoryMapper.findById(id);
    }

    /**
     * 根据id删除分类
     * @param id
     */
    @Override
    public void delete(Integer id) {
        memoCategoryMapper.deleteById(id);
    }
}
